import 'package:get/get.dart';
import 'package:flutter/material.dart';
import '../../../../service/HttpsClient.dart';
import '../../../../utils/storage/storage.dart';

class RegisterStepThreeController extends GetxController {
  //TODO: Implement RegisterStepThreeController
  TextEditingController passwordController = TextEditingController();
  TextEditingController confirmPasswordController = TextEditingController();
  HttpsClient httpsClient = HttpsClient();
  // 去注册
  void goRegister() async {
    String password = passwordController.text;
    String confirmPassword = confirmPasswordController.text;
    if (password != confirmPassword) {
      Get.snackbar("提示", "两次密码不一致");
      return;
    }
    if (password.length < 6) {
      Get.snackbar("提示", "密码长度不能小于6位");
      return;
    }
    var response = await httpsClient.post(
      "api/register",
      data: {
        "tel": Get.arguments["tel"],
        "password": password,
        "code": Get.arguments["code"],
      },
    );
    print("response: $response");
    if(response!=null){
      // 存入本地缓存
      await Storage.setData("userinfo", response["userinfo"]);
      if(response["success"]) {
        Get.offAllNamed("/tabs", arguments: {"index": 4});  // 关闭所有页面，跳转到tabs页面，并让initialPage为用户页面
      }else {
        Get.snackbar("提示", response["message"]);
      }
    }else {
      Get.snackbar("提示", "网络错误");
    }
  }

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
